//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"


//高阶函数
//基础函数
func fi (x: Int) -> Int {
    return 2 * x
}

func F (f: Int -> Int) -> Int -> Int {
    func rect(x: Int) -> Int {
        return 2 * f(x)
    }
    return rect
}

let f01 = F(fi)
f01(10)


//1.1
func f02(x: Int, _ y: Int) -> Int {
    return x + 2 * y
}

func F02(x : Int) -> Int -> Int {
    func rect(y: Int) -> Int {
        return x + 2 * y
    }
    return rect
}

let f02Text = F02(1)
f02Text(10)

//柯里化，看onevcat的swift tips
//指令式编程中的函数是一种子程序。
//函数式编程的特征：
//1.函数是一等函数，可以像其他数据类型出现在任何地方
//2。无副作用
//3.所有都是表达式，没有for，switch等状态控制
//4.

//阶乘的数学递归定义：0! = 1, n! = (n-1)! * n;
func factorial(n: Int) -> Int {
    return n == 0 ? 1 : factorial(n - 1) * n
}

factorial(10)
factorial(5)


//Swift设计模式（老汤，曾伟）
class MySelf {
    static var sharedInstance : MySelf?
    var name = "xiaoming"
    init(){
        if MySelf.sharedInstance == nil {
            MySelf.sharedInstance = self
        }
    }
}

let mySelfSingelObject = MySelf.init()

[1, 2, 3].reduce(1, combine: +)

//Monad,唐巧
let arr01 = [1, 2, 3, 4]
let arr02 = [5, 6, 7, 8]
let brr1 = arr01.map({ return "NO." + String($0) })
let abArr01 = [arr01, arr02]
let brr02 = abArr01.flatMap( { $0 } )
print(brr02)

let a: Int? = 1
let b = a! + 1

let brr03 = abArr01.flatMap { (a : [Int]) -> [Int] in
    print("A")
    return  a.flatMap({ $0 })
}
print(brr03)


//How To Parse Float Number
let array001 = (1...9).flatMap({ x in
    return (1...9).map({ y in
        return x * y
    })
})

//github:
//github.com/lingoer/SwiftyCharms
//DSL   //标记语言


